package com.pan.hisystem.service.impl;

import com.pan.hisystem.pojo.Outpatients;
import com.pan.hisystem.pojo.PatientTest;
import com.pan.hisystem.mapper.PatientTestMapper;
import com.pan.hisystem.pojo.RespBean;
import com.pan.hisystem.service.IOutpatientsService;
import com.pan.hisystem.service.IPatientTestService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author qingyang
 * @since 2022-04-02
 */
@Service
public class PatientTestServiceImpl extends ServiceImpl<PatientTestMapper, PatientTest> implements IPatientTestService {

    @Autowired
    private IOutpatientsService outpatientsService;
    @Autowired
    private PatientTestMapper patientTestMapper;
    @Autowired
    private IPatientTestService patientTestService;

    /**
     * 添加化验单
     * @param id
     * @param oid
     * @return
     */
    @Override
    @Transactional
    public RespBean addTest(Integer id, Integer oid) {
        //先添加化验单，获取化验id，再将化验id绑定到就诊表
        if(oid==null){
            return RespBean.error("添加失败!");
        }
        PatientTest patientTest=new PatientTest();
        patientTest.setCreateTime(LocalDateTime.now());
        patientTest.setNameId(id);
        patientTest.setPayStatus(false);
        patientTest.setStatus(false);
        patientTest.setBackStatus(false);
        if(patientTestService.save(patientTest)){
            System.out.println(patientTest.getId());
            System.out.println(oid);
            Outpatients outpatients=new Outpatients();
            outpatients.setId(oid);
            outpatients.setTid(patientTest.getId());
            if(outpatientsService.updateById(outpatients)){
                return RespBean.success("添加成功!");
            }
            return RespBean.error("添加失败!");
        }
        return RespBean.error("添加失败!");

    }

    /**
     * 根据化验id获取化验信息
     * @return
     */
    @Override
    public RespBean getTestMes(Integer id) {
        PatientTest patientTest=patientTestMapper.getTestMes(id);
        if(patientTest==null){
            return RespBean.error("无该化验单!");
        }
        if(!patientTest.getPayStatus()){
            return RespBean.error("化验单未付费，请先付费");
        }
        if(!patientTest.getStatus()){
            return RespBean.error("该患者并未进行化验，请先前往化验!");
        }
        if(patientTest.getBackStatus()){
            return RespBean.error("该化验单已退费!");
        }

        return RespBean.success("获取成功!",patientTest);

    }

    /**
     * 根据卡号获取化验结果
     * @return
     */
    @Override
    public RespBean getTestResultByMid(String mid) {
        PatientTest patientTest=patientTestMapper.getTestResultByMid(mid);
        if(patientTest==null){
            return RespBean.error("获取失败!");
        }
        return RespBean.success("获取成功!");
    }
}
